package sanmubird.club.mock.dao;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import sanmubird.club.mock.dao.beans.UserConverter;
import sanmubird.club.mock.dao.beans.UserConverterImpl;

import javax.sql.DataSource;

/**
 * @author sam
 * @date 2019/12/9 8:52
 * @description 功能描述
 */
@Configuration
@ComponentScan(basePackageClasses = UserDaoTestConfig.class)
public class UserDaoTestConfig {

	@Bean(destroyMethod = "shutdown")
	public DataSource dataSource() {
		EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
				.generateUniqueName(true)
				.setType(EmbeddedDatabaseType.H2)
				.setScriptEncoding("UTF-8")
				.ignoreFailedDrops(true)
				.addScript("h2/user_schema.sql")
				.addScripts("h2/user_data.sql")
				.build();

		return db;
	}

	@Bean
	public JdbcTemplate jdbcTemplate() {
		return new JdbcTemplate(dataSource());
	}

	@Bean
	public UserConverter userConverter() {
		return new UserConverterImpl();
	}
}
